#include "p18f4550.h"
#include "Rotabit.h"
#include <delays.h>

unsigned char power(unsigned char state)
{
	
	switch(state)
	{
	case 0:
	return 1;
	break;

	case 1:
	return 2;
	break;

	case 2:
	return 4;
	break;

	case 3:
	return 8;
	break;

	case 4:
	return 16;
	break;

	case 5:
	return 32;
	break;

	case 6:
	return 64;
	break;

	case 7:
	return 128;
	break;

	default:
	return 255;
	break;
	}	
}




void rotabit_init()
{//initializeADC();
count=0;
	state=0;
input=0;
//initializeADC();
LATC=0;
LATD=(unsigned char)power(state);	
}
 void rotabit_run()
{
//	LATC=PORTC;
if(count==0)
{
ADCON0bits.GO_DONE = 1;//Start A/D Conversion
while(ADCON0bits.GO_DONE );//Loop here until A/D conversion completes
delay = ADRESH;
input=PORTC&0X03;
state=transitions[state][input];
LATD=power(state);
temporal_result=ADRESH/20;

count++;
}
else{count++;
	if(count>=temporal_result)
	{

	count=0;
	}
	}
//Delay1KTCYx(ADRESH);

}
